package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 新增分类
     *
     * @param categoryDTO
     * @return
     */
    @Override
    public void add(CategoryDTO categoryDTO) {
        Category category = new Category();
        //复制分类
        BeanUtils.copyProperties(categoryDTO, category);
        //新分类默认不开启
        category.setStatus(StatusConstant.DISABLE);
//        //设置时间
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//        //创建者与更新者
//        category.setCreateUser(BaseContext.getCurrentId());
//        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.insert(category);
    }

    /**
     * 启用、禁用分类
     *
     * @param status
     * @param id
     * @return
     */
    @Override
    public void enableOrDisable(Integer status, Long id) {
        Category category = Category.builder()
                .status(status)
                .id(id).build();
        categoryMapper.update(category);
    }

    /**
     * 分类分页查询
     *
     * @param categoryPageQueryDTO 查询参数
     * @return
     */
    @Override
    public PageResult queryByPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        //PageHelper设置
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        Page<Category> page = categoryMapper.queryByPage(categoryPageQueryDTO.getName(), categoryPageQueryDTO.getType());
        PageResult pageResult = new PageResult();
        //返回查询数据
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());
        return pageResult;
    }

    /**
     * 编辑分类信息
     *
     * @param categoryDTO
     * @return
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
//        //更新修改时间
//        category.setUpdateTime(LocalDateTime.now());
//        //修改更新者
//        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.update(category);
    }

    /**
     * 根据id删除分类
     *
     * @param id
     * @return
     */
    @Override
    public void deleteById(Long id) {
        //根据id来查询分类是否关联了菜品
        Integer count  = dishMapper.countById(id);
        if(count > 0){
            //如果关联则抛异常
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        //根据id来查询分类是否关联了套餐
        count = setmealMapper.countById(id);

        if(count > 0){
            //如果关联则抛异常
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        //没有关联则可删除
        categoryMapper.deleteById(id);
    }

    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> queryByType(Integer type) {
        List<Category>categoryList = categoryMapper.queryByType(type);
        return categoryList;
    }
}
